<?php
/*
+--------------------------------------------------------------------------
|   phpBIZ v3.0  full version
|   ========================================
|   by taft@wjl.cn yejun@wjl.cn
|   http://www.phpbiz.cn http://www.wjl.cn
|   all rights reserved
+---------------------------------------------------------------------------
|
|   > 最后修改日期：2005-4-30 2006-2-8
|
+--------------------------------------------------------------------------
*/
!class_exists('Debug') && exit('Forbidden');
$fav = new favors;
class favors
{
	function favors()
	{
		global $BIZ,$INCOME;

		if ( empty($BIZ->seller['seller_id']) or $BIZ->seller['seller_id'] == "" or $BIZ->seller['seller_id'] == 0 )
		{
			$BIZ->error( "用户没有登陆，不能使用此功能","?act=Login&code=00" );
		}

		$BIZ -> assign("code","viewf");

		switch($INCOME[code])
		{
			case 'add':
				$this -> add_favors();
			    break;
			case 'delf':
				$this -> del_favors();
			    break;
			case 'viewf':
				$BIZ-> nav_link[] = array("我的收藏夹","");
			default:
				$this -> view_favors();
				break;
		}

		$BIZ -> output("User.tpl");
	}

	function add_favors()
	{
		global $BIZ,$INCOME,$DB;
		
		$gid = $INCOME[GOODS_ID];
		if(empty($gid)||!is_numeric($gid))
		{
			$BIZ -> error('错误：参数非法');
		}

		$DB -> db_query("SELECT gids FROM biz_favors WHERE uid=".$BIZ->seller[seller_id]);
		$rs = $DB -> db_fetch_row();
		if($rs)
		{
			$gids=$rs['gids'];
			$favornum=50;
			$gid_db=explode(",",$gids);
			if(count($gid_db)>$favornum){
				$BIZ -> error('错误：您添加的收藏商品已超过50件。');
			}
			$olddb=','.$gids.',';
			if(strpos($olddb,','.$gid.',')!==false){
				$BIZ -> error('错误：您已收藏了该商品。');
			}
			$gids.=','.$gid;
			$DB->db_query("UPDATE biz_favors SET gids='$gids' WHERE uid=".$BIZ->seller[seller_id]);
		}else
		{
			$DB->db_query("INSERT INTO biz_favors(uid,gids) VALUES('{$BIZ->seller[seller_id]}','$gid')");
		}

		$BIZ ->redirect_screen("恭喜！收藏成功",'?act=favors');
	}
	function view_favors()
	{
		global $BIZ,$INCOME,$DB;

		$DB->db_query("SELECT gids FROM biz_favors WHERE uid=".$BIZ->seller[seller_id]);	
		$favor = $DB -> db_fetch_row();
		if($favor['gids'])
		{
			$DB->db_query("SELECT g.goods_name,g.goods_id,g.goods_store_date FROM biz_goods AS g WHERE g.goods_id IN($favor[gids]) ORDER BY g.goods_store_date DESC");
			
			while($favor=$DB -> db_fetch_row())
			{
				$favor[goods_store_date] = date("Y-m-d H:i",$favor[goods_store_date]);
				$f[] = array($favor[goods_name],$favor[goods_id],$favor[goods_store_date]);
			}

			$BIZ -> assign("f",$f);
			

		}
	}

	function del_favors()
	{
		global $INCOME,$BIZ,$DB;
		$delid = $INCOME[delf];
		$DB->db_query("SELECT gids FROM biz_favors WHERE uid=".$BIZ->seller[seller_id]);
		$rs = $DB -> db_fetch_row();
		if($rs)
		{
			$gids=$rs['gids'];
			$gid_db=explode(",",$gids);
			$g_count=count($gid_db);
			for($i=0;$i<$g_count;$i++){
				if(@in_array($gid_db[$i],$delid)){
					unset($gid_db[$i]);
				}
			}
			$new_gids=implode(",",$gid_db);
			if($new_gids!=$gids){
				if($new_gids){
					$DB->db_query("UPDATE biz_favors SET gids='$new_gids' WHERE uid=".$BIZ->seller[seller_id]);
				}else{
					$DB->db_query("DELETE FROM biz_favors WHERE uid=".$BIZ->seller[seller_id]);
				}
				$BIZ ->redirect_screen("删除成功",'?act=favors');
			}else{
				$BIZ -> error('错误：没有指定删除收藏的商品!');
			}
		}else{
			$BIZ -> error('错误：没有指定删除收藏的商品!');
		}
	}
}
?>